package com.asiainfo.ssm.execute.dao.impl;

import java.rmi.RemoteException;
import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.asiainfo.ssm.execute.bo.BOExecPluginDefBean;
import com.asiainfo.ssm.execute.bo.BOExecPluginDefEngine;
import com.asiainfo.ssm.execute.dao.interfaces.IExecPluginDefDAO;
import com.asiainfo.ssm.execute.ivalues.IBOExecPluginDefValue;

public class ExecPluginDefDAOImpl implements IExecPluginDefDAO {
	public static transient Log log = LogFactory.getLog(ExecPluginDefDAOImpl.class);
	 

	 

	public long savePlugin(IBOExecPluginDefValue execPluginDefValue)
			throws Exception, RemoteException {
 		long pluginId=0;
		if(execPluginDefValue.getPluginId()==0){
			pluginId=BOExecPluginDefEngine.getNewId().longValue();
			execPluginDefValue.setPluginId(pluginId);
			execPluginDefValue.setEffectDate(BOExecPluginDefEngine.getSysDate());
		}
		else{
			pluginId=execPluginDefValue.getPluginId();
		}
		BOExecPluginDefEngine.save(execPluginDefValue);
		return pluginId;
	}


	
	public IBOExecPluginDefValue getExecPluginDefValue(long pluginId) throws Exception{
		return BOExecPluginDefEngine.getBean(pluginId);
	}
    
	


	@Override
	public IBOExecPluginDefValue[] getPluginDefList(long criticalPointId) throws Exception, RemoteException {
		String sql = "STATE = 1 and CRITICAL_POINT_ID = :criticalPointId";
		HashMap para = new HashMap();
		para.put("criticalPointId", criticalPointId);
		BOExecPluginDefBean[] beans = BOExecPluginDefEngine.getBeans(sql, para);
		return beans;
	}
 
	 
	@Override
	public IBOExecPluginDefValue getPluginDefByCriticalPointId(long criticalPointId) throws Exception, RemoteException {
		String sql = "select * from EXEC_PLUGIN_DEF WHERE STATE =1   and CRITICAL_POINT_ID = :criticalPointId AND EFFECT_DATE < SYSDATE AND EXPIRE_DATE > SYSDATE";
		HashMap parameter = new HashMap();
		parameter.put("criticalPointId", criticalPointId);
		BOExecPluginDefBean[] beans = BOExecPluginDefEngine.getBeansFromSql(sql, parameter);
		if(beans !=null && beans.length > 0){
			return beans[0];
		}else{ 
			return null;
		}
	}

	
	@Override
	public IBOExecPluginDefValue[] getPluginDefs() throws Exception, RemoteException {
		String sql = "select * from EXEC_PLUGIN_DEF WHERE STATE =1  AND EFFECT_DATE < SYSDATE AND EXPIRE_DATE > SYSDATE";
		HashMap parameter = new HashMap(); 
		BOExecPluginDefBean[] beans = BOExecPluginDefEngine.getBeansFromSql(sql, parameter);
		return beans;
	}

}
